package cn.tedu.fruitshop.controller;

import cn.tedu.fruitshop.pojo.dto.InformAddNewDTO;
import cn.tedu.fruitshop.pojo.dto.InformUpdateInfoDTO;
import cn.tedu.fruitshop.pojo.vo.InformListItemVO;
import cn.tedu.fruitshop.service.InformService;
import cn.tedu.fruitshop.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/inform")
@Api(tags = "05. 公告管理模块")
public class InformController {
    @Autowired
    InformService informService;
    public InformController(){
        log.debug("创建控制器对象：InformController");
    }

    @ApiOperation("添加公告")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(InformAddNewDTO informAddNewDTO){
        log.debug("开始处理【添加公告】的请求，参数：{}", informAddNewDTO);
        informService.addNew(informAddNewDTO);
        log.debug("添加公告成功！");
        return JsonResult.ok();
    }
    @ApiOperation("根据id删除公告")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiImplicitParam(name = "id", value = "公告id", required = true, dataType = "Integer")
    public JsonResult<Void>delete(@PathVariable Integer id){
        log.debug("开始处理【删除公告】的请求，参数：{}", id);
        informService.delete(id);
        log.debug("删除公告成功！");
        return JsonResult.ok();
    }
    @ApiOperation("查询公告列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<InformListItemVO>>list(){
        log.debug("开始处理【查询公告列表】的请求，无参数");
         List<InformListItemVO> list=informService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("根据id修改公告详情")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "公告id", required = true, dataType = "Integer")
    @PostMapping("/{id:[0-9]+}/updateById")
    public JsonResult<Void> updateById(@PathVariable Integer id, InformUpdateInfoDTO informUpdateInfoDTO){
        log.debug("开始处理【修改公告详情】的请求，参数ID：{}, 新数据：{}", id, informUpdateInfoDTO);
        informService.updateInfoById(id,informUpdateInfoDTO);
        return JsonResult.ok();
    }

}
